package com.lxx.wiki.whitelist;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpMethod;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 鉴权白名单的处理
 */
@Component
@ConfigurationProperties(prefix = "white-list")
public class WhiteList {

    private List<String> urlsForGet = new ArrayList<>();
    private List<String> urlsForPost = new ArrayList<>();

    private RequestMatcher[] requestMatchers = new RequestMatcher[0];

    public List<String> getUrlsForGet() {
        return urlsForGet;
    }

    public List<String> getUrlsForPost() {
        return urlsForPost;
    }

    public RequestMatcher[] getRequestMatchers() {
        List<RequestMatcher> requestMatchers = new ArrayList<>();
        urlsForGet.stream().map(url -> new AntPathRequestMatcher(url, HttpMethod.GET.name())).forEach(requestMatchers::add);
        urlsForPost.stream().map(url -> new AntPathRequestMatcher(url, HttpMethod.POST.name())).forEach(requestMatchers::add);
        return requestMatchers.toArray(RequestMatcher[]::new);
    }
}
